<template>
  <div class="xn-text-tabs">
    <div
      v-for="(item, idx) in tabs"
      :key="item.key"
      class="tab-item"
      :class="{ actived: item.key === activedSelf }"
      @click="changeTabType(item.key, idx)">
      {{ item.label }}
    </div>
  </div>
</template>

<script>
export default {
  name: 'XnTextTab',
  model: {
    prop: 'actived',
    event: 'valueChanged',
  },
  props: {
    tabs: {
      type: Array,
    },
    actived: {
      type: String,
    },
  },
  data() {
    return {
      activedSelf: '',
      activedIdx: 0,
    }
  },
  watch: {
    actived(val) {
      this.activedSelf = val
    },
    activedSelf(val) {
      this.$emit('valueChanged', val)
      this.$emit('change', val)
      this.$emit('index-change', this.activedIdx)
    },
  },
  mounted() {
    console.log('xn tabs...mounted')
    if (this.tabs && this.tabs.length) {
      this.activedSelf = this.actived || this.tabs[0].key
    }
  },
  methods: {
    changeTabType(key, idx) {
      if (this.activedSelf !== key) {
        this.activedSelf = key
        this.activedIdx = idx
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.xn-text-tabs {
  padding: 0 18px;
  display: flex;
  align-items: center;
  height: 50px;
  .tab-item {
    font-size: 18px;
    font-weight: 400;
    color: #666666;
    cursor: pointer;
    transition: 0.3s;
    &.actived {
      font-size: 24px;
      font-weight: bold;
      color: #333333;
    }
    &:not(:last-child) {
      &::after {
        content: '';
        width: 1px;
        height: 23px;
        background: #dcdcdc;
        display: inline-block;
        margin: 0 25px;
        vertical-align: middle;
        margin-top: -5px;
      }
    }
  }
}
</style>
